

import threading
from time import sleep

# 线程锁
lock = threading.Lock()

local = threading.local()

num = 0


def run(str):
    global num

    for i in range(1000000):
        # 加锁
        lock.acquire()
        try:
            num = num + 1

        finally:
            # 释放锁
            lock.release()


if __name__ == "__main__":
    t1 = threading.Thread(target=run, args=('ok',))
    t2 = threading.Thread(target=run, args=('ok',))

    t1.start()
    t2.start()

    t1.join()
    t2.join()

    print("当前的num =", num)

